package org.zcl.mongo.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.zcl.mongo.entity.User;
import org.zcl.mongo.service.UserService;

import java.util.List;

/**
 * .
 *
 * @author zhuchenglin
 * @since 2024/8/5 13:56
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    public MongoTemplate mongoTemplate;


    public List<User> findUsersByNameLike(String name) {
        Query query = new Query();
        query.addCriteria(Criteria.where("name").regex(name, "i")); // 使用正则表达式进行模糊查询，"i" 表示不区分大小写
        return mongoTemplate.find(query, User.class);
    }
}
